package com.yedean.mall.controller;

import com.yedean.mall.api.CommonResult;
import com.yedean.mall.controller.form.UmsMemberLoginForm;
import com.yedean.mall.entity.UmsMember;
import com.yedean.mall.service.IUmsMemberService;
import com.yedean.mall.token.TokenGroup;
import com.yedean.mall.token.TokenRedisClient;
import com.yedean.mall.token.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@Api(tags = "前台会员登录接口")
@RequestMapping("/portal/umsMember")
public class UmsMemberController {
    
    @Autowired
    IUmsMemberService umsMemberService;
    @Autowired
    TokenRedisClient redisClient;
    
    
    @GetMapping("/getAuthCode/{telephone}")
    @ApiOperation("获取验证码")
    public CommonResult<?> getAuthCode(@PathVariable String telephone){
      return null;
    }
    
    @PostMapping("/login")
    @ApiOperation(("模拟用户登录"))
    public CommonResult<?> login(@RequestBody @Valid UmsMemberLoginForm form){
        String username = form.getUsername();

        String password = form.getPassword();

        boolean exist = umsMemberService.memberExist(username);
        if (!exist){
            return CommonResult.failed("账号不存在");
        }
        boolean check = umsMemberService.memberCheck(username, password);
        if (!check){
            return CommonResult.failed("账号密码错误");
        }
        
        //获取用户信息
        UmsMember umsMember = umsMemberService.getMember(username);
        
        //生成token
        TokenGroup tokenGroup = redisClient.askToken(new UserInfo(umsMember.getNickname(), umsMember.getMemberLevelId(), System.currentTimeMillis()));
        
        return CommonResult.success(tokenGroup);

    } 
    
    
    
    
    
    
    
    
    
    
    
}
